#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#define BUFFER_SIZE 1024

// 函数声明
void list_directory(const char *path);
void create_file(const char *path);
void delete_file(const char *path);
void copy_file(const char *source, const char *destination);
void move_file(const char *source, const char *destination);
void create_directory(const char *path);
void delete_directory(const char *path);
void change_directory(char *current_path);

int main()
{
    char current_path[BUFFER_SIZE];
    if (getcwd(current_path, sizeof(current_path)) == NULL)
    {
        perror("获取当前目录失败");
        return EXIT_FAILURE;
    }
    printf("当前目录: %s\n", current_path);

    int choice;
    while (1)
    {
        printf("\n=== 文件管理器菜单 ===\n");
        printf("1. 列出目录内容\n");
        printf("2. 创建新文件\n");
        printf("3. 删除文件\n");
        printf("4. 复制文件\n");
        printf("5. 移动/重命名文件\n");
        printf("6. 创建新目录\n");
        printf("7. 删除目录\n");
        printf("8. 进入子目录\n");
        printf("9. 返回上级目录\n");
        printf("10. 退出\n");
        printf("请选择操作 (1-10): ");
        if (scanf("%d", &choice) != 1)
        {
            printf("无效的输入，请输入数字1-10。\n");
            // 清除输入缓冲区
            while (getchar() != '\n');
            continue;
        }

        char source[BUFFER_SIZE];
        char destination[BUFFER_SIZE];

        switch (choice)
        {
        case 1:
            list_directory(current_path);
            break;
        case 2:
            printf("输入要创建的文件名: ");
            scanf("%s", source);
            strcpy(destination, current_path);
            strcat(destination, "/");
            strcat(destination, source);
            create_file(destination);
            break;
        case 3:
            printf("输入要删除的文件名: ");
            scanf("%s", source);
            strcpy(destination, current_path);
            strcat(destination, "/");
            strcat(destination, source);
            delete_file(destination);
            break;
        case 4:
            printf("输入源文件名: ");
            scanf("%s", source);
            strcpy(destination, current_path);
            strcat(destination, "/");
            strcat(destination, source);
            printf("输入目标文件名: ");
            char target[BUFFER_SIZE];
            scanf("%s", target);
            char dest_path[BUFFER_SIZE];
            strcpy(dest_path, current_path);
            strcat(dest_path, "/");
            strcat(dest_path, target);
            copy_file(destination, dest_path);
            break;
        case 5:
            printf("输入源文件名: ");
            scanf("%s", source);
            strcpy(destination, current_path);
            strcat(destination, "/");
            strcat(destination, source);
            printf("输入目标文件名: ");
            scanf("%s", target);
            char move_dest[BUFFER_SIZE];
            strcpy(move_dest, current_path);
            strcat(move_dest, "/");
            strcat(move_dest, target);
            move_file(destination, move_dest);
            break;
        case 6:
            printf("输入要创建的目录名: ");
            scanf("%s", source);
            strcpy(destination, current_path);
            strcat(destination, "/");
            strcat(destination, source);
            create_directory(destination);
            break;
        case 7:
            printf("输入要删除的目录名: ");
            scanf("%s", source);
            strcpy(destination, current_path);
            strcat(destination, "/");
            strcat(destination, source);
            delete_directory(destination);
            break;
        case 8:
            // 进入子目录
            printf("输入要进入的目录名: ");
            scanf("%s", source);
            strcpy(destination, current_path);
            strcat(destination, "/");
            strcat(destination, source);
            struct stat st;
            if (stat(destination, &st) == 0 && S_ISDIR(st.st_mode))
            {
                strcpy(current_path, destination);
                printf("当前目录已更新为: %s\n", current_path);
            }
            else
            {
                printf("无法进入目录: %s\n", destination);
            }
            break;
        case 9:
            // 返回上级目录
            if (strcmp(current_path, "/") == 0)
            {
                printf("已经在根目录，无法返回上级目录。\n");
            }
            else
            {
                char *last_slash = strrchr(current_path, '/');
                if (last_slash != current_path)
                {
                    *last_slash = '\0';
                }
                else
                {
                    strcpy(current_path, "/");
                }
                printf("当前目录已更新为: %s\n", current_path);
            }
            break;
        case 10:
            printf("退出文件管理器。\n");
            exit(0);
        default:
            printf("无效的选择，请输入1-10。\n");
        }
    }

    return 0;
}

void list_directory(const char *path)
{
    DIR *dir;
    struct dirent *entry;

    dir = opendir(path);
    if (dir == NULL)
    {
        perror("无法打开目录");
        return;
    }

    printf("目录内容:\n");
    while ((entry = readdir(dir)) != NULL)
    {
        printf("%s\n", entry->d_name);
    }

    closedir(dir);
}

void create_file(const char *path)
{
    FILE *fp = fopen(path, "w");
    if (fp == NULL)
    {
        perror("无法创建文件");
        return;
    }
    fclose(fp);
    printf("文件 %s 创建成功\n", path);
}

void delete_file(const char *path)
{
    if (remove(path) != 0)
    {
        perror("无法删除文件");
    }
    else
    {
        printf("文件 %s 删除成功\n", path);
    }
}

void copy_file(const char *source, const char *destination)
{
    FILE *fs, *fd;
    int ch;

    fs = fopen(source, "rb");
    if (fs == NULL)
    {
        perror("无法打开源文件");
        return;
    }

    fd = fopen(destination, "wb");
    if (fd == NULL)
    {
        perror("无法创建目标文件");
        fclose(fs);
        return;
    }

    while ((ch = fgetc(fs)) != EOF)
    {
        fputc(ch, fd);
    }

    fclose(fs);
    fclose(fd);
    printf("文件复制成功\n");
}

void move_file(const char *source, const char *destination)
{
    if (rename(source, destination) != 0)
    {
        perror("无法移动/重命名文件");
    }
    else
    {
        printf("文件移动/重命名成功\n");
    }
}

void create_directory(const char *path)
{
    if (mkdir(path, 0777) != 0)
    {
        perror("无法创建目录");
    }
    else
    {
        printf("目录 %s 创建成功\n", path);
    }
}

void delete_directory(const char *path)
{
    if (rmdir(path) != 0)
    {
        perror("无法删除目录");
    }
    else
    {
        printf("目录 %s 删除成功\n", path);
    }
}

void change_directory(char *current_path)
{
    char destination[BUFFER_SIZE];
    printf("输入要进入的目录名: ");
    scanf("%s", destination);

    // 构建新路径
    char new_path[BUFFER_SIZE];
    strcpy(new_path, current_path);
    strcat(new_path, "/");
    strcat(new_path, destination);

    struct stat st;
    if (stat(new_path, &st) == 0 && S_ISDIR(st.st_mode))
    {
        strcpy(current_path, new_path);
        printf("当前目录已更新为: %s\n", current_path);
    }
    else
    {
        perror("无法进入目录");
    }
}